#include "GenericRepository.h"

template<class TEntity>
GenericRepository<TEntity>::GenericRepository(string fileName)
{
    loadData();                                                 
    this->entities = new LinkedList<TEntity>();                                                 
    this->fileName = fileName;                 
}

template<class TEntity>
LinkedList<TEntity>* GenericRepository<TEntity>::get()
{
    return entities;           
}

template<class TEntity>
void GenericRepository<TEntity>::add(TEntity *entity)
{
    entities->add(entity);
    saveChanges();
}
             
template<class TEntity>
void GenericRepository<TEntity>::edit(TEntity *entity, int index)
{
    entities->edit(index,entity);
    saveChanges();
}

template<class TEntity>             
void GenericRepository<TEntity>::remove(int index)
{
     entities->remove(index);
     saveChanges();
}

template<class TEntity>
void GenericRepository<TEntity>::saveChanges()
{
     stream(fileName, ios::binary | ios::out);
     stream.write(reinterpret_cast<char*>(entities),sizeof(LinkedList<TEntity>));
     stream.close();
}

template<class TEntity>
void GenericRepository<TEntity>::loadData()
{
     stream(fileName, ios::binary | ios::in);    
     stream.read(reinterpret_cast<char*>(entities),sizeof(LinkedList<TEntity>));
     stream.close();
}    
